package com.yanghui.service.impl;

import com.yanghui.dao.LinkMapper;
import com.yanghui.pojo.Link;
import com.yanghui.pojo.LinkExample;
import com.yanghui.service.LinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class LinkServiceImpl implements LinkService {
    @Autowired
    private LinkMapper linkMapper;


    /**
     * 更新友情链接点击次数
     */
    @Override
    public int updateClickNumById(Integer id) {
        Link link = linkMapper.selectByPrimaryKey(id);
        link.setClickNum(link.getClickNum() + 1);

        return linkMapper.updateByPrimaryKeySelective(link);
    }

    /**
     * 查询所有友情链接
     * 已经上架的
     */
    @Override
    public List<Link> selectAllLinks() {
        LinkExample linkExample = new LinkExample();
        LinkExample.Criteria criteria = linkExample.createCriteria();
        criteria.andStatusEqualTo(1);
        criteria.andIsApplyEqualTo(1);

        return linkMapper.selectByExample(linkExample);
    }

    /**
     * 查询未处理友情链接数量
     */
    @Override
    public int selectNoApplyLinksCount() {
        LinkExample linkExample = new LinkExample();
        LinkExample.Criteria criteria = linkExample.createCriteria();
        /** isApply = -1 代表未处理的友情链接 */
        criteria.andIsApplyEqualTo(-1);
        List<Link> linkList = linkMapper.selectByExample(linkExample);
        /** 未处理的友情链接数量 */
        int count = linkList.size();

        return count;
    }

    /**
     * 添加友情链接
     * 需要考虑是否与某个链接重复
     */
    @Override
    public int addLink(Link link) {
        LinkExample linkExample = new LinkExample();
        LinkExample.Criteria criteria = linkExample.createCriteria();
        criteria.andNameEqualTo(link.getName());
        List<Link> linkList = linkMapper.selectByExample(linkExample);
        if (linkList.size() > 0) {
            return -1;
        }
        return linkMapper.insertSelective(link);
    }

    /**
     * 根据Id删除友情链接
     */
    @Override
    public int deleteLinkById(Integer id) {
        return linkMapper.deleteByPrimaryKey(id);
    }

    /**
     * 更新友情链接
     */
    @Override
    public int updateLinkById(Link link) {
        return linkMapper.updateByPrimaryKeySelective(link);
    }

    /**
     * 根据Id查询友情链接
     */
    @Override
    public Link selectLinksById(Integer id) {
        return linkMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据查询条件查询友情链接
     */
    @Override
    public List<Link> selectLikeLinksListByPage(Map<String, Object> map) {
        LinkExample linkExample = new LinkExample();
        LinkExample.Criteria criteria = linkExample.createCriteria();
        if (map.get("name") != null) {
            criteria.andNameLike("%" + map.get("name") + "%");
        }
        if (map.get("status") != null) {
            criteria.andStatusEqualTo((Integer) map.get("status"));
        }
        if (map.get("isApply") != null) {
            criteria.andIsApplyEqualTo((Integer) map.get("isApply"));
        }

        return linkMapper.selectByExample(linkExample);
    }

    /**
     * 按照状态查询友情链接数量
     */
    @Override
    public List<?> selectLinksListByStatus() {
        return linkMapper.selectLinksListByStatus();
    }

}
